$(function () {
    var ordCode = $("#ordCode");
    var ordName = $("#ordName");
    var count = $("#count");
    var unit = $("#unit");
    var totalPrice = $("#totalPrice");
    var isPay = $("#isPay");
    var describe = $("#describe");
    var supId = $("#supId");
    var saveOrd = $("#saveOrd");
    var addOrdForm = $("#addOrdForm");

    $.ajax({
        type : "GET",
        url : getPath() + "/JSP/ord.go",
        data : {method : "getSupNames"},
        dateType: "json",
        success:function (data) {
            if (data != null){
                supId.html("");
                var options = "<option value=\"0\">--请选择--</option>";
                for (let i = 0; i < data.length; i++) {
                    options += "<option value=\"" + data[i].id + "\">" + data[i].supName + "</option>";
                }
                supId.html(options);
                setStyles(supId.next(), {'color':'red'}, getImgCross() + "(请选择供应商)", false);
            }
        },
        error:function (data) {
            setStyles(supId.next(), {'color':'red'}, getImgCross() + "(请求错误)", false);
        }
    });

    ordCode.on({
        focus:function () {
            setStyles(ordCode.next(), {'color':'red'}, "(输入编号,不能为空)", false);
        },
        blur:function () {
            $.ajax({
                type: "GET",
                url: getPath() + "/JSP/ord.go",
                data: {method: "ordCodeExist", ordCode:ordCode.val()},
                success:function (data) {
                    if (data.ordCode == "exist"){
                        setStyles(ordCode.next(), {'color':'red'}, "(编号存在)", false);
                    }else {
                        setStyles(ordCode.next(), {'color':'#87CEED'}, "(OK)", true);
                    }
                },
                error:function (data) {
                    setStyles(ordCode.next(), {'color':'red'}, "(请求错误)", false);
                }
            })
        }
    });

    ordName.on({
        focus:function () {
            setStyles(ordName.next(), {'color':'red'}, "(输入姓名,不能为空)", false);
        },
        blur:function () {
            var len = ordName.val().length;
            if (len > 0){
                setStyles(ordName.next(), {'color':'#87CEEB'}, "(OK)", true);
            }else {
                setStyles(ordName.next(), {'color':'red'}, "(不能为空)", false);
            }
        }
    });

    count.on({
        focus:function () {
            setStyles(count.next(), {'color':'red'}, "(输入商品数量,不能为空)", false);
        },
        blur:function () {
            var len = count.val().length;
            if (len > 0){
                setStyles(count.next(), {'color':'#87CEEB'}, "(OK)", true);
            }else {
                setStyles(count.next(), {'color':'red'}, "(不能为空)", false);
            }
        }
    });

    unit.on({
        focus:function () {
            setStyles(unit.next(), {'color':'red'}, "(输入商品数量单位,不能为空)", false);
        },
        blur:function () {
            var len = unit.val().length;
            if (len > 0){
                setStyles(unit.next(), {'color':'#87CEEB'}, "(OK)", true);
            }else {
                setStyles(unit.next(), {'color':'red'}, "(不能为空)", false);
            }
        }
    });

    totalPrice.on({
        focus:function () {
            setStyles(totalPrice.next(), {'color':'red'}, "(输入金额,不能为空)", false);
        },
        blur:function () {
            var len = totalPrice.val().length;
            if (len > 0){
                setStyles(totalPrice.next(), {'color':'#87CEEB'}, "(OK)", true);
            }else {
                setStyles(totalPrice.next(), {'color':'red'}, "(不能为空)", false);
            }
        }
    });

    totalPrice.on({
        focus:function () {
            setStyles(totalPrice.next(), {'color':'red'}, "(输入金额,不能为空)", false);
        },
        blur:function () {
            var len = totalPrice.val().length;
            if (len > 0){
                setStyles(totalPrice.next(), {'color':'#87CEEB'}, "(OK)", true);
            }else {
                setStyles(totalPrice.next(), {'color':'red'}, "(不能为空)", false);
            }
        }
    });

    isPay.on({
        blur:function () {
            var len = isPay.val().length;
            console.log(len);
            if (len > 2){
                setStyles(isPay.next(), {'color':'#87CEEB'}, "(OK)", true);
            }else {
                setStyles(isPay.next(), {'color':'red'}, "(请选择)", false);
            }
        }
    });

    supId.on({
        blur:function () {
            var v = supId.val();
            if (v.toString() != "0"){
                setStyles(supId.next(), {'color':'#87CEEB'}, "(OK)", true);
            }else {
                setStyles(supId.next(), {'color':'red'}, "(请选择)", false);
            }
        }
    });


    saveOrd.on({
        click:function (){
            console.log(ordCode.attr("validateStatus"));
            console.log(ordName.attr("validateStatus"));
            console.log(count.attr("validateStatus"));
            console.log(unit.attr("validateStatus"));
            console.log(totalPrice.attr("validateStatus"));
            console.log(isPay.attr("validateStatus"));
            console.log(supId.attr("validateStatus"));
            if(ordCode.attr("validateStatus") != "true"){
                ordCode.blur();
            }else if(ordName.attr("validateStatus") != "true"){
                ordName.blur();
            }else if(count.attr("validateStatus") != "true"){
                count.blur();
            }else if(unit.attr("validateStatus") != "true"){
                unit.blur();
            }else if(totalPrice.attr("validateStatus") != "true"){
                totalPrice.blur();
            }else if(isPay.attr("validateStatus") != "true"){
                isPay.blur();
            }else if(supId.attr("validateStatus") != "true"){
                supId.blur();
            }else{
                if(confirm("是否确认提交数据")){
                    addOrdForm.submit();
                }
            }
        }
    });


})